package com.ddc.study.feature.main.home;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ddc.study.R;
import com.ddc.study.entity.Article;
import com.ddc.study.feature.main.tree.detail.TreeDetailActivity;

/**
 * Created on 2018/4/4.
 *
 * @author lshun
 * @version 1.0
 */
class HomeAdapter extends BaseQuickAdapter<Article, BaseViewHolder> {
    HomeAdapter() {
        super(R.layout.item_home);
    }

    @Override
    protected void convert(BaseViewHolder helper, Article item) {
        helper.setText(R.id.tv_title, item.getTitle())
                .setText(R.id.tv_author, item.getAuthor())
                .setText(R.id.tv_time, item.getNiceDate())
//                .addOnClickListener(R.id.cb_favorite)
//                .setChecked(R.id.cb_favorite, item.isCollect())
                .addOnClickListener(R.id.iv_favorite);
        ImageView iv = helper.getView(R.id.iv_favorite);
        iv.setSelected(item.isCollect());
        iv.setContentDescription(item.isCollect() ? "取消收藏" : "");

        setClassInfo(helper.getView(R.id.tv_class), item);
    }

    private void setClassInfo(TextView textView, final Article item) {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(item.getSuperChapterName());
        builder.append(" / ");
        builder.append(item.getChapterName());

        //获取colorPrimary
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(getColorPrimary(textView.getContext()));
        UnderlineSpan underlineSpan = new UnderlineSpan();

        int start = 0, end = start + item.getSuperChapterName().length();
        builder.setSpan(colorSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        builder.setSpan(underlineSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        builder.setSpan(new MyClickSpan(item.getSuperChapterId(), item.getSuperChapterName()),
                start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        start = item.getSuperChapterName().length() + 3;
        end = start + item.getChapterName().length();
        builder.setSpan(colorSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        builder.setSpan(underlineSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        builder.setSpan(new MyClickSpan(item.getChapterId(), item.getChapterName()),
                start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        textView.setText(builder);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private int getColorPrimary(Context context) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        return typedValue.data;
    }

    private static final class MyClickSpan extends ClickableSpan {
        private final int mChapterId;
        private final String mChapterName;

        MyClickSpan(int chapterId, String chapterName) {
            this.mChapterId = chapterId;
            this.mChapterName = chapterName;
        }

        @Override
        public void onClick(View widget) {
            TreeDetailActivity.openDetail(widget.getContext(), mChapterId, mChapterName);
        }
    }
}
